let { auth } = require("./utils/auth") // 引入auth中间件（短token的验证）
const express = require("express");
const app = express();
const gulogin = require("./routes/Gjx_index");
const cors = require("cors");
const list = require("./routes/Lym_list");
const zzshequ = require("./routes/zz_cate");
const mshome = require("./routes/ms_home");
const zzpinglun = require("./routes/zz_catepl");
const zzliwu = require("./routes/zz_liwu");
const ms_home = require("./routes/ms_home");
const zhifu = require("./routes/zhifu");
const dong = require("./routes/dong");
const up=require('./routes/up')
app.use(cors());
app.use(express.json());
app.use(auth) //配置中间件来验证短token的有效性
app.use(list);
app.use(up);
app.use(zzshequ);
app.use(mshome);
app.use(zzpinglun);
app.use(zzliwu);
app.use(ms_home);
app.use(zhifu);
app.use(gulogin);
app.use(dong);
app.use("/uploads", express.static("uploads"));
app.listen(8000, () => {
  console.log("server is running on port 8000");
});
const WebSocket = require('ws');
let url = require('url')
const wss = new WebSocket.Server({ port: 8080 });
let arr = []
wss.on('connection', (ws, req) => {
    ws.id = url.parse(req.url, true).query.id
    // 监听消息
    ws.on('message', (message) => {
        let data=JSON.parse(message)
        console.log('发送者的信息',data)
        wss.clients.forEach(client => {
            client.send(JSON.stringify({send_name:data.name,send_id:data.use_id,accepted_id:data.send_id}))
        });
    });

    // 监听连接关闭
    ws.on('close', () => {
        console.log('Client disconnected');
    });

    // 监听连接错误
    ws.on('error', (error) => {
        console.error(`Error: ${error}`);
    });

    // 向所有客户端广播消息
});



